package com.luckycoder.controller;

import com.luckycoder.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/hello")
public class HelloHandle {

    @RequestMapping("/index")
    public String index() {
        System.out.println("index run ...");
        return "index";
    }

    //指定请求方式；HandleAdapter 自动会将请求参数转换为行参
    @RequestMapping(value = "/index1", method = RequestMethod.GET, params = {"name", "id=10"})
    public String index1(String name) {
        System.out.println(name);
        System.out.println("index1 执行了……");
        return "index";
    }

    //当形参与请求参数名字不同时，使用 @RequestParam 注解显示为形参指定请求参数
    @RequestMapping(value = "/index2", method = RequestMethod.GET, params = {"name", "id=10"})
    public String index2(@RequestParam("name") String str,@RequestParam("id") int id) {
        System.out.println(str);
        System.out.println(id);
        System.out.println("index2 执行了……");
        return "index";
    }

    //REST 风格
    @RequestMapping("/rest/{name}/{id}")
    public String rest(@PathVariable String name, @PathVariable int id) {
        System.out.println("REST ……");
        System.out.println(name);
        System.out.println(id);
        return "index";
    }

    @RequestMapping("/cookie")
    public String cookie(@CookieValue(value = "JSESSIONID") String session){
        System.out.println(session);
        return "index";
    }

    @RequestMapping("/register")
    public String register(User user) {
        System.out.println(user.getId());
        System.out.println(user.getName());
        System.out.println(user.getAddress());
        return "index";
    }
}
